<?php

namespace app\api\server\vip;

use app\api\model\music\MusicSelectedPackApiModel;
use app\api\model\UserModel;
use app\api\model\vip\VipApiModel;
use app\api\model\vip\OrderVipApiModel;
use app\api\ServerBase;
use app\logic\model\user\UserLogicModel;
use app\logic\single\pay\AliPaySingle;
use app\logic\single\pay\WxPaySingle;
use app\logic\single\UserScoreSingle;
use Snow\Snow;
use think\facade\Db;
use think\Request;

class VipServer extends ServerBase
{

    /**
     * @var array|mixed
     */
    private $param;
    /**
     * @var VipApiModel
     */
    private $model;
    private $default_field='id,title,img,privilege,expire_time,price,introduce,selected_pack';
    /**
     * @var VipOrderApiModel
     */
    private $orderModel;

    function __construct(Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new VipApiModel();
        $this->orderModel = new OrderVipApiModel();
    }
    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        $this->param['status']=1;
        [$list,$count] = $this->model->getApiList($this->param,[],$this->default_field);
        if($this->request->user){
            $vip_id = $this->request->user['vip_id'];
        }else{
            $vip_id = -1;
        }
        foreach ($list as &$val){
            $selected_pack_field = 'title,play_num,price,sale,img';
            if(!empty($val['selected_pack'])){
                if($vip_id == $val['id']){
                    $selected_pack_field.=',pan_url,pan_pwd';
                }
                $val['selected_pack'] = MusicSelectedPackApiModel::whereIn('id',$val['selected_pack'])
                    ->field($selected_pack_field)->select()->toArray();
            }else{
                $val['selected_pack']=[];
            }
            unset($val);
        }
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }

    function buy(){
        $id = $this->param['id']??0;
        if(empty($id)){
            $this->getMessageCode('code_common.illegal',1);
            return false;
        }
        $info = $this->model->where('status',1)
            ->where('id',$id)->find();
        if(empty($info)){
            $this->getMessageCode('code_common.illegal',1);
            return false;
        }
        $user_vip = $this->model->where('id',$this->request->user['vip_id'])->find();
        if(!empty($user_vip)){
            if($user_vip['level'] > $info['level']){
                $this->ERROR='当前已是更高等级vip';
                return false;
            }
        }
        //创建订单
        Db::startTrans();
        try {
            $order=[
                'vip_id'=>$info['id'],
                'order_no'=>Snow::createOnlyId(),
                'buyer_id'=>$this->request->user['id'],
                'order_price'=>$info['price'],
                'pay_price'=>$info['price'],
            ];
            $this->orderModel->save($order);
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR=$exception->getMessage();
            return false;
        }
        $user = UserLogicModel::getInfoById($order['buyer_id']);
        $order['openid'] = $user['openid'];
        //获取支付信息
        $terminal = $this->param['terminal']??'h5';
        //todo 微信支付
        $res = WxPaySingle::getInstance()->pay($order,$terminal,'vip','购买'.$info['title']);
        $data['wx_'.$terminal] = $res;

        $res= AliPaySingle::getInstance()->pay(
            $order,
            $terminal,
            'vip',
            '购买'.$info['title'],
            $this->param['ali_quit_url']??'',
            $this->param['ali_return_url']??'',
        );
        $data['ali_'.$terminal] = $res;

        return $data;
    }
}